using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Repository;
using JTZY.Service.Goods.IGoodsService;

namespace JTZY.Service.Goods
{
    /// <summary>
    /// 货品表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICorecmsproductsService), ServiceLifetime = LifeTime.Transient)]
    public class CorecmsproductsService : BaseService<Corecmsproducts>, ICorecmsproductsService
    {
        /// <summary>
        /// 查询货品表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CorecmsproductsDto> GetList(CorecmsproductsQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                //.Includes(x => x.CorecmsagentproductsNav) //填充子对象
                .Where(predicate.ToExpression())
                .ToPage<Corecmsproducts, CorecmsproductsDto>(parm);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Corecmsproducts GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加货品表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Corecmsproducts AddCorecmsproducts(Corecmsproducts model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改货品表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCorecmsproducts(Corecmsproducts model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Corecmsproducts> QueryExp(CorecmsproductsQueryDto parm)
        {
            var predicate = Expressionable.Create<Corecmsproducts>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Sn), it => it.Sn == parm.Sn);
            return predicate;
        }
    }
}